#!/bin/bash
if grep -Eqi "16.10" /etc/*-release || echo "${Ubuntu_Version}" | grep -Eqi '^16.10'; then
        Ros_ver='kinetic'
elif grep -Eqi "16.04" /etc/*-release || echo "${Ubuntu_Version}" | grep -Eqi '^16.04'; then
        Ros_ver='kinetic'
elif grep -Eqi "18.04" /etc/*-release || echo "${Ubuntu_Version}" | grep -Eqi '^18.04'; then
        Ros_ver='melodic'
elif grep -Eqi "18.10" /etc/*-release || echo "${Ubuntu_Version}" | grep -Eqi '^18.10'; then
        Ros_ver='melodic'
fi
video_user=$( getent passwd |grep 1000 | head -n 1 | cut -d : -f 1)
video_gp=$(getent group video |grep $video_user |wc -l)
if [ $video_gp -eq 0 ];then
        sudo usermod -aG video $video_user
fi
nuc_suspend=$(sudo systemctl status sleep.target suspend.target hibernate.target hybrid-sleep.target|grep enabled |wc -l)
if [ $nuc_suspend -ne 0 ];then
        sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target
fi
nginxurl=/opt/openresty/nginx
golo_ros_ver=$(cat $nginxurl/scripts/golo.conf |grep ROS_PATH |awk -F "/" '{print $4}')
sudo sed -i "s/$golo_ros_ver/$Ros_ver/" $nginxurl/scripts/golo.conf

ip=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v 172.17.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"|awk 'NR==1'`
byip=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v 172.17.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"|cut -d \. -f 1-3|awk 'NR==1'`
if [ "$ip" = "" ]; then
	     exit 1
     fi
nuchost=$(cat /etc/hosts |grep 127.0.1.1 |awk '{print $2}')
nucdns=$(cat /etc/resolv.conf |grep nameserver |awk '{print $2}')
golo_ver=$( cat $nginxurl/scripts/golo.conf |grep GOLO_Ver |tr -d "GOLO_Ver=") 
golo_ver=$(echo $golo_ver|sed 'N;s/\n//g')    
golo_ip=$( cat $nginxurl/scripts/golo.conf |grep ROSIP |tr -d "ROSIP="|awk -F "[\'\']" '{print $2}')
ssh_ip=$(cat $nginxurl/scripts/golo.conf |grep SSHIP |tr -d "SSHIP="|awk -F "[\'\']" '{print $2}'|cut -d \. -f 1-3)
golo_cid=$( cat $nginxurl/scripts/golo.conf |grep cid |tr -d "cid=")
golo_token=$( cat /usr/local/src/frp/frpc.ini |grep token |tr -d "token="|sed 's/^[ ]*//g')
golo_frpurl=$( cat /usr/local/src/frp/frpc.ini |grep server_addr |awk -F "=" '{print $2}'|sed 's/^[ ]*//g')
rtk_ip=$( cat $nginxurl/scripts/golo.conf |grep RTKIP |tr -d "RTKIP="|awk -F "[\'\']" '{print $2}'|cut -d \. -f 1-3)
htmlip=$( cat $nginxurl/html/t.html |grep "var ip2"|awk -F "=" '{print $2}'|awk -F "[\"\"]" '{print $2}')
. $nginxurl/scripts/golo.conf
cd $GOLO_WS_PATH/../../
if [  -d ".golo_ws" ]
    then
    websocket_cid=$( cat $GOLO_WS_PATH/golo/websocket-client/scripts/config_cid.py |grep cid |tr -d "cid=" |awk '{print $1}'|sed 's/^[ ]*//g') && chmod 777 $GOLO_WS_PATH/../../.golo_ws/src -R
    else
    websocket_cid=$( cat $GOLO_WS_PATH/golo/websocket-client/scripts/config_cid.py |grep cid |tr -d "cid=" |awk '{print $1}'|sed 's/^[ ]*//g')&& sudo sed -i "s#.golo_ws#golo_ws#g" $nginxurl/scripts/golo.conf && sudo sed -i "s#.rtk_ws#rtk_ws#g" $nginxurl/scripts/golo.conf && sudo sed -i "s#.golo_ws#golo_ws#g" $nginxurl/scripts/.init && sudo sed -i "s#.rtk_ws#rtk_ws#g" $nginxurl/scripts/.init && chmod 777 $GOLO_WS_PATH/../../golo_ws/src -R
    fi
api_url=$( cat $GOLO_WS_PATH/golo/websocket-client/scripts/config.py | grep http_url |awk -F "//" '{print $2}' |awk -F "/" '{print $1}' )
www_url=$( cat $GOLO_WS_PATH/golo/websocket-client/scripts/config.py | grep socket_url |awk -F "//" '{print $2}' |awk -F ":" '{print $1}' )
api_url_ip=`ping ${api_url} -c 1 | sed '1{s/[^(]*(//;s/).*//;q}'`
www_url_ip=`ping ${www_url} -c 1 | sed '1{s/[^(]*(//;s/).*//;q}'`
sudo sed -i "s#$golo_ip#$ip#g" $nginxurl/scripts/golo.conf
sudo sed -i "s#$rtk_ip#$byip#g" $nginxurl/scripts/golo.conf
sudo sed -i "s#$ssh_ip#$byip#g" $nginxurl/scripts/golo.conf
sudo sed -i "s#$htmlip#$ip#g" $nginxurl/html/t.html
sudo sed -i "s#$golo_cid#$websocket_cid#g" $nginxurl/scripts/golo.conf
sudo sed -i "s#$golo_token#$websocket_cid#g" /usr/local/src/frp/frpc.ini
sudo sed -i "s#$golo_frpurl#$Nxserver_Frpurl#g" /usr/local/src/frp/frpc.ini
sudo sed -i "s/$nuchost/$ip/" /etc/hosts
sudo sed -i "$a $www_url_ip $www_url" /etc/hosts
sudo sed -i "1c $ip" /etc/hostname
sudo sed -i "s/$nucdns/119.29.29.29/" /etc/resolv.conf
sudo sed -i "/119.29.29.29/a nameserver 223.6.6.6" /etc/resolv.conf
sudo sed -i  "/192.168/d" /etc/hosts.allow
sudo sed -i "1a sshd:$SSHIP:allow" /etc/hosts.allow
sudo sed -i "1a sshd:192.168.8.234:allow" /etc/hosts.allow
sudo sed -i  "/api/d" /etc/hosts
sudo sed -i  "/www/d" /etc/hosts
sudo sed -i 's/rosnode_num=0/rosnode_num=1/g' /opt/openresty/nginx/scripts/golo.conf
rtkodom_init=$( cat /etc/ros/melodic/goloR.d/rtkodom.launch |grep rtk_odom|wc -l )
if [ $rtkodom_init -eq 1 ];then
sudo sed -i 's/init_coord.yaml/init_coord_map.yaml/g' /opt/ros/melodic/share/rtk_odom/launch/rtkodom.launch
else
sudo sed -i 's/init_coord.yaml/init_coord_map.yaml/g' /opt/ros/melodic/share/jz_driver/launch/unicore.launch
fi
if [ ! -z $www_url_ip ];then
sudo sed -i "5a $www_url_ip $www_url  " /etc/hosts
fi
if [ ! -z $api_url_ip ];then
sudo sed -i "5a $api_url_ip $api_url  " /etc/hosts
fi
if [ $Nxserver_Code == "0" ];then
	     sudo systemctl restart nxserver && sudo sed -i 's/Nxserver_Code=0/Nxserver_Code=1/g' /opt/openresty/nginx/scripts/golo.conf
     fi
     if [ $Nxserver_Frp == "0" ];then
	          sudo systemctl restart frpc && sudo sed -i 's/Nxserver_Frp=0/Nxserver_Frp=1/g' /opt/openresty/nginx/scripts/golo.conf
	  fi
source /opt/ros/$Ros_ver/setup.bash 
sudo systemctl start goloM
min=1
max=22
while [ $min -le $max ]
do
source /opt/ros/$Ros_ver/setup.bash 
ip_route=`ip route show default | awk '{print $3}'`
rosWD=$(ps -ef |grep goloWD-start |grep -v grep |wc -l)
if [ $rosWD == 0 ];then
       sleep 2
       sudo systemctl start goloWD
fi
rosR=$(ps -ef |grep goloR-start |grep -v grep |wc -l)
sudo chmod 777 /home/nuc/.rtk_ws -R
if [ $rosR == 0 ];then
    sleep 1
    sudo systemctl start goloR
fi
rosD=$(ps -ef |grep goloD-start |grep -v grep |wc -l)
if [ $rosD == 0 ];then
   sudo systemctl start goloD
fi
rosF=$(ps -ef |grep goloF-start |grep -v grep |wc -l)
if [ $rosF == 0 ];then
    sleep 2
    sudo systemctl start goloF
fi
rosN=$(ps -ef |grep goloN-start |grep -v grep |wc -l)
goloN_rtk=$(cat /etc/ros/melodic/goloN.d/bluertk.launch|grep bluertk|wc -l)
if [ $goloN_rtk == 0 ];then
       sudo sed -i 's#blueraslam.launch#bluertk.launch#' /etc/ros/melodic/goloN.d/bluertk.launch
fi

if [ $rosN == 0 ];then
    sleep 1
    sudo systemctl start goloN
fi
rosSN=$(ps -ef |grep goloSN-start |grep -v grep |wc -l)
if [ $rosSN == 0 ];then
	sleep 1
	sudo systemctl start goloSN
    fi
rosDV=$(ps -ef |grep goloDV-start |grep -v grep |wc -l)
goloDVM_gnss=$(cat /etc/ros/melodic/goloDVM.d/fix_map.launch|grep fix_map|wc -l)
if [ $goloDVM_gnss == 0 ];then
       sudo sed -i 's#gnss_map.launch#fix_map.launch#' /etc/ros/melodic/goloDVM.d/fix_map.launch
fi
if [ $rosDV == 0 ];then
    sleep 1
    sudo systemctl start goloDV
fi
rosW=$(ps -ef |grep goloW3-start |grep -v grep |wc -l)
if [ $rosW == 0 ];then
    sleep 1
    sudo systemctl start goloW3
    ping -c 2 $ip_route > /dev/null
if [ $? -ne 0 ];then
	sudo netplan apply
fi
fi
rosRL=$(ps -ef |grep goloRL-start |grep -v grep |wc -l)
if [ $rosRL == 0 ];then
	sleep 2
	sudo systemctl start goloRL
fi
rosC=$(ps -ef |grep goloC-start |grep -v grep |wc -l)
if [ $rosC == 0 ];then
	sleep 2
 sudo chmod 777 /dev/video*  && sudo systemctl start goloC
fi
rosWO=$(ps -ef |grep goloWO-start |grep -v grep |wc -l)
if [ $rosWO == 0 ];then
    sleep 2
   sudo systemctl start goloWO
fi
min=$(rosnode list |grep -v rosout|grep -v talker_node|grep -v echibot*|grep -v dynparam* |grep -v rostopic*|wc -l)
if [ ${min} == ${max} ];then
 break
fi
done
while true
do
rosWDN=$(rosnode list|grep websocketclient_data |wc -l)
rosRN=$(rosnode list|grep rtk_odom |wc -l)
rosDN=$(rosnode list|grep xiang_tu_driver |wc -l)
rosFN=$(rosnode list|grep flow_manager_node |wc -l)
rosWON=$(rosnode list|grep ota_client |wc -l)
rosRLN=$(rosnode list|grep location_validator_node |wc -l)
rosDV=$(rosnode list|grep dynamic_vwall |wc -l)
rosSNN=$(rosnode list|grep simple_nav_goals |wc -l)
rosNN=$(rosnode list|grep move_base |wc -l)
rosWN=$(rosnode list|grep websocketclientv3 |grep -v websocketclient_data |wc -l)
rosMN=$(rosnode list|grep talker_node |wc -l)
rosCN=$(rosnode list|grep mo_camera |wc -l)
rosWMN=$(rosnode list|grep golo_monitor |wc -l)
if [ $rosMN == 0 ];then
	          sudo systemctl restart goloM
fi
if [ $rosCN == 0 ];then
   sudo chmod 777 /dev/video*  && sudo systemctl restart goloC
fi
if [ $rosWN == 0 ];then
	sleep 2
                  sudo systemctl restart goloW
fi
if [ ! -f "$GOLO_WS_PATH/golo/websocket-client/scripts/real_monitor_v3.py" ];then
	break
fi
sudo sed -i 's/real_monitor.py/real_monitor_v3.py/g' /home/nuc/.golo_ws/src/golo/websocket-client/launch/monitor.launch
if [ $rosMN == 1 ] && [ $rosNN == 1 ] && [ $rosWDN == 1 ] && [ $rosWN == 1 ];then
	sudo systemctl start goloWM && sudo sed -i 's/rosnode_num=1/rosnode_num=0/g' /opt/openresty/nginx/scripts/golo.conf
fi
if [ $rosWMN == 1 ];then
	   break
fi
done


